#include<ctype.h>
#include<stdio.h>

void newLine();

	void enterWait() __critical
	{	
		printf_tiny("  Hit <ENTER to continue>");
		while(getchar()!=13){
		}
		
		printf_tiny("\033[2J"); 
	}
	

int countChar(char * val){ 			//counts the number of characters in the char
	
	int i=0;
	int size=0;
	while (val[i]!='\n'){
		size++;
		i++;
	}
	size=size-1;
	return size;
}


int isAllDigit(char * val){ 	//returns 1 if user has entered non-numbers anywhere in the input,else 0
	int i=0;
	int d=0;

	while((i<countChar(val))&&(d==0)&&(val[i]!='\n')){
		if(isdigit(val[i])==0){ //execute the if loop only if result of isdigit is non-zero
			d=1;
			
					
		}
	i++;
	
	}
	
	return d;
}
	
/*empties buffer: reassigns working pointer to base pointer, demarcation on subsequent entries provided by NULL character*/	
char * emptyBuffer(char * val,char * src){ 
	int i=0;
	int space=0; 
	val=src;
	while (val[i]!='\n'){
		printf("%c\t",val[i]);
		i++;
		space++;
		if (space==10){
			printf_tiny("\r\n");
			space=0;
		}
	}
	printf_tiny("\n\r\t\t");
	enterWait();
	printf_tiny("\n\n\n\t\t\t");
	val=src;
	return val;
}

//if input parameter n is 1, converts any lower character to upper. For all other n values, does the opposite
void upperLower(char *val,int n){
	int i=0;
		
	while(val[i]!='\n'){
		if (n==1){		
			val[i]=toupper(val[i]);
		}
		else{
		       val[i]=tolower(val[i]); 
		}
		i++;
	}
}



/*performs required tasks when "=" is pressed*/
void printHex(char *val){
	
	int i=0;
	int space=0;
	newLine();
	printf("Current Contents in Buffer0:\r\n%04x : ",(int)(val));
		while(val[i]!='\n')
		{
			printf("%x ",val[i]); //to print 16 bytes per line
			if (space==15)
			{
			printf("\r\n%04x : ",(int)(val+16));
			space=0;
			}
			i++;
			space++;
			
		}
		printf_tiny("\r\n\t\t\tYou can continue entering values\n\r");
		space=0;
	}
	
	
	//provides statistics on the number of times 0-9 and A-Z occurred in the given buffer
	void calStats(char *val){
		int number=48; //ASCII Value of 0
		int space=0;
		int upperAlpha=65; 	//ASCII Value of A
		int counter=0;		//counter for tracking number of occurences
		while(number<=57){	//57 is the ASCII of 9
			int i=0;
			while(val[i]!='\n'/*&&isdigit(val[i])!=0*/){	//check if control has not reached end of buffer  

			if(number==val[i]){	//if value in buffer is equal to what we are looking for
				counter++;
				}
			i++;
			}
		printf("(%c : %d)\t|\t",number,counter);
		space++;
		if(space==4){
			printf_tiny("\r\n");
			space=0;
		}
		counter=0; //restart counter
		number++; //increment the ASCII to point to next number 
		}





		while(upperAlpha<=90){ //ASCII of Z, the maximum limit
			int i=0;
			while(val[i]!='\n'/*&&isupper(val[i])!=0*/){ //check if end of buffer has not been reached

			if(upperAlpha==val[i]){ 	//if value in buffer is equal to what we are looking for
				counter++;
				}
			i++;
			}
		printf("(%c : %d)\t|\t",upperAlpha,counter);
		space++;
		if(space==4){
			printf_tiny("\r\n");
			space=0;
		}
		counter=0;
		upperAlpha++; ////increment the ASCII to point to next alphabet
		}


	}
	
	